package Chess_Flyweight_12;

import Chess_Flyweight_12.Flyweight.BlackFlyweight;
import Chess_Flyweight_12.Flyweight.WhiteFlyweight;

import java.util.HashMap;
import java.util.Map;

public class ChessFactory {
    static Map<String, ChessPiece> chessPieces = new HashMap<>();

    //如果不存在，则创建一个新的对应颜色的棋子实例
    //如果存在，则返回已存在的实例
    public static ChessPiece getChessPiece(String color) {
        if (!chessPieces.containsKey(color)) {
            if (color.equals("白")) {
                chessPieces.put(color, new WhiteFlyweight());
                return chessPieces.get(color);
            } else if (color.equals("黑")) {
                chessPieces.put(color, new BlackFlyweight());
                return chessPieces.get(color);
            } else {
                throw new IllegalArgumentException("未知的颜色: " + color);
            }
        }
        return chessPieces.get(color);
    }
}
